/////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2005 - Hooyberghs Johnny
//
// Distribute and change freely, but please don't remove my name from the source 
//
// No warrantee of any kind, express or implied, is included with this
// software; use at your own risk, responsibility for damages (if any) to
// anyone resulting from the use of this software rests entirely with the
// user. 
//
// questions?
// Feel free to contact me: johnny.hooyberghs@gmail.com
//

// dependencies
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace GDFlacTools.Controls.Panels
{
	[ToolboxBitmapAttribute(typeof(Panel),"ExDotNet.ico") ]
	public class Panel : System.Windows.Forms.Panel
	{
		private System.ComponentModel.Container components = null;

		#region Gradient
		private Color m_color_StartColor = Color.FromKnownColor( KnownColor.InactiveCaptionText );
		private Color m_color_EndColor = Color.FromKnownColor( KnownColor.InactiveCaption );
		private System.Drawing.Drawing2D.LinearGradientMode m_GradientMode = System.Drawing.Drawing2D.LinearGradientMode.Vertical;

		[Description("The starting color of the gradient background"),Category("_Gradient"),Browsable(true)]
		public Color GradientStart
		{
			get
			{
				return m_color_StartColor;
			}
			set
			{
				m_color_StartColor = value;
				Invalidate();
			}
		}

		[Description("The end color of the gradient background"),Category("_Gradient"),Browsable(true)]
		public Color GradientEnd
		{
			get
			{
				return m_color_EndColor;
			}
			set
			{
				m_color_EndColor = value;
				Invalidate();
			}
		}

		[Description("The gradient direction"),Category("_Gradient"),Browsable(true)]
		public System.Drawing.Drawing2D.LinearGradientMode GradientDirection
		{
			get
			{
				return m_GradientMode;
			}
			set
			{
				m_GradientMode = value;
				Invalidate();
			}
		}
		#endregion

		#region Border
		private GDFlacTools.Controls.BorderStyle m_BorderStyle = GDFlacTools.Controls.BorderStyle.Single;
		private int m_int_BorderWidth = 1;
		private bool m_bool_Border = true;
		private Color m_color_BorderColor = Color.FromKnownColor( KnownColor.ActiveCaption );

		[Description("The style of the border"),Category("_Border"),Browsable(true)]
		public GDFlacTools.Controls.BorderStyle Style
		{
			get
			{
				return m_BorderStyle;
			}
			set
			{
				m_BorderStyle = value;
				Invalidate();
			}
		}

		[Description("The width in pixels of the border"),Category("_Border"),Browsable(true)]
		public int BorderWidth
		{
			get
			{
				return m_int_BorderWidth;
			}
			set
			{
				m_int_BorderWidth = value;
				Invalidate();
			}
		}

		[Description("Enable/Disable border"),Category("_Border"),Browsable(true)]
		public bool Border
		{
			get
			{
				return m_bool_Border;
			}
			set
			{
				m_bool_Border = value;
				Invalidate();
			}
		}

		[Description("The color of the border"),Category("_Border"),Browsable(true)]
		public Color BorderColor
		{
			get
			{
				return m_color_BorderColor;
			}
			set
			{
				m_color_BorderColor = value;
				Invalidate();
			}
		}

		#endregion

		#region Caption

		private String m_str_Caption = "Panel";
		private bool m_bool_Caption = true;
		private int m_int_CaptionHeight = 24;
		private Color m_color_CaptionBeginColor = Color.FromArgb(255, 225, 155);
		private Color m_color_CaptionEndColor = Color.FromArgb(255, 165, 78);
		private Color m_color_CaptionTextColor = Color.FromArgb( 0 , 0 , 0 );
		private bool m_bool_Antialias = true;
		private System.Drawing.Drawing2D.LinearGradientMode m_CaptionGradientMode = System.Drawing.Drawing2D.LinearGradientMode.Vertical;
		private System.Drawing.StringAlignment m_StringAlignment = System.Drawing.StringAlignment.Near;

		[Description("The gradient direction"),Category("_Caption"),Browsable(true)]
		public System.Drawing.StringAlignment CaptionTextAlignment
		{
			get
			{
				return m_StringAlignment;
			}
			set
			{
				m_StringAlignment = value;
				Invalidate();
			}
		}

		[Description("The gradient direction"),Category("_Caption"),Browsable(true)]
		public System.Drawing.Drawing2D.LinearGradientMode CaptionGradientDirection
		{
			get
			{
				return m_CaptionGradientMode;
			}
			set
			{
				m_CaptionGradientMode = value;
				Invalidate();
			}
		}

		[Description("Enable/Disable antialiasing"),Category("_Caption"),Browsable(true)]
		public bool TextAntialias
		{
			get
			{
				return m_bool_Antialias;
			}
			set
			{
				m_bool_Antialias = value;
				Invalidate();
			}
		}

		[Description("The caption"),Category("_Caption"),Browsable(true)]
		public String CaptionText
		{
			get
			{
				return m_str_Caption;
			}
			set
			{
				m_str_Caption = value;
				Invalidate();
			}
		}

		[Description("Enable/Disable the caption"),Category("_Caption"),Browsable(true)]
		public bool Caption
		{
			get
			{
				return m_bool_Caption;
			}
			set
			{
				m_bool_Caption = value;
				Invalidate();
			}
		}

		[Description("Change the caption height"),Category("_Caption"),Browsable(true)]
		public int CaptionHeight
		{
			get
			{
				return m_int_CaptionHeight;
			}
			set
			{
				m_int_CaptionHeight = value;
				Invalidate();
			}
		}

		[Description("Change the caption begincolor"),Category("_Caption"),Browsable(true)]
		public Color CaptionBeginColor
		{
			get
			{
				return m_color_CaptionBeginColor;
			}
			set
			{
				m_color_CaptionBeginColor = value;
				Invalidate();
			}
		}

		[Description("Change the caption endcolor"),Category("_Caption"),Browsable(true)]
		public Color CaptionEndColor
		{
			get
			{
				return m_color_CaptionEndColor;
			}
			set
			{
				m_color_CaptionEndColor = value;
				Invalidate();
			}
		}



		[Description("Change the caption textcolor"),Category("_Caption"),Browsable(true)]
		public Color CaptionTextColor
		{
			get
			{
				return m_color_CaptionTextColor;
			}
			set
			{
				m_color_CaptionTextColor = value;
				Invalidate();
			}
		}

		[Description("Change the caption textcolor"),Category("_Caption"),Browsable(true)]
		public override Font Font
		{
			get
			{
				return base.Font;
			}
			set
			{
				base.Font = value;
				Invalidate();
			}
		}


		#endregion

		#region Icon

		private System.Drawing.Icon m_Icon = null;
		private bool m_bool_Icon = false;

		[Description("The icon to display in the title"),Category("_Icon"),Browsable(true)]
		public Icon PanelIcon
		{
			get
			{
				return m_Icon;
			}
			set
			{
				m_Icon = value;
				Invalidate();
			}
		}

		[Description("Enable/Disable the icon"),Category("_Icon"),Browsable(true)]
		public bool IconVisible
		{
			get
			{
				return m_bool_Icon;
			}
			set
			{
				m_bool_Icon = value;
				Invalidate();
			}
		}

		#endregion

		#region Constructor
		public Panel()
		{
			InitializeComponent();

			this.Font = new Font( "Arial" , 14 );

			this.SetStyle( ControlStyles.UserPaint , true );
			this.SetStyle( ControlStyles.AllPaintingInWmPaint , true );
			this.SetStyle( ControlStyles.DoubleBuffer , true );
		}
		#endregion

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{

		}
		#endregion

		#region Painting
		protected override void OnPaint(PaintEventArgs e)
		{
			// The painting with shadow is slightly different...
			if( m_BorderStyle == GDFlacTools.Controls.BorderStyle.Shadow )
			{
				// fill the background
				System.Drawing.Drawing2D.LinearGradientBrush brsh = new System.Drawing.Drawing2D.LinearGradientBrush( new Rectangle( 0 , 0 , this.Width-5 , this.Height-5 ) , m_color_StartColor , m_color_EndColor , m_GradientMode );
				e.Graphics.FillRectangle( brsh , 0 , 0 , this.Width-5 , this.Height-5 );

				// draw the border
				System.Drawing.Pen pen = new Pen( m_color_BorderColor );
				for( int i=0 ; i<m_int_BorderWidth ; i++ )
				{
					e.Graphics.DrawRectangle( pen , i , i , this.Width - 6 - (i*2) , this.Height - 6 - (i*2) );
				}

				// draw the caption bar
				if( m_bool_Caption )
				{
					if( m_bool_Antialias ) e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
					System.Drawing.Drawing2D.LinearGradientBrush brsh_Caption = new System.Drawing.Drawing2D.LinearGradientBrush( new Rectangle( m_int_BorderWidth , m_int_BorderWidth , this.Width - 5 - (m_int_BorderWidth*2) , m_int_CaptionHeight ) , m_color_CaptionBeginColor , m_color_CaptionEndColor , m_CaptionGradientMode );
					e.Graphics.FillRectangle( brsh_Caption , m_int_BorderWidth , m_int_BorderWidth  , this.Width - 5 - (m_int_BorderWidth*2) , m_int_CaptionHeight );
					StringFormat format = new StringFormat();
					format.FormatFlags = StringFormatFlags.NoWrap;
					format.LineAlignment = StringAlignment.Center;
					format.Alignment = m_StringAlignment;
					format.Trimming = StringTrimming.EllipsisCharacter;
					e.Graphics.DrawString( 
						m_str_Caption , 
						this.Font , 
						new SolidBrush( m_color_CaptionTextColor ) , 
						new Rectangle( 
						// LEFT
						(m_BorderStyle != GDFlacTools.Controls.BorderStyle.None ) ? 
						(m_bool_Icon ? 
						m_int_BorderWidth + m_Icon.Width + ( ( m_int_CaptionHeight / 2 ) - ( m_Icon.Height / 2 ) ) 
						: 
						m_int_BorderWidth)
						: 
						(m_bool_Icon ? 
						m_Icon.Width + ( ( m_int_CaptionHeight / 2 ) - ( m_Icon.Height / 2 ) ) 
						: 
						0) , 
						// TOP
						(m_BorderStyle != GDFlacTools.Controls.BorderStyle.None ) ?  
					m_int_BorderWidth 
						: 
						0 ,
						// WIDTH
						(m_BorderStyle != GDFlacTools.Controls.BorderStyle.None ) ?  
						(m_bool_Icon ? 
						this.Width - (m_int_BorderWidth*2) - ( ( m_int_CaptionHeight / 2 ) - ( m_Icon.Height / 2 ) ) - m_Icon.Width -5
						: 
						this.Width - (m_int_BorderWidth*2))-5
						:
						(m_bool_Icon ?
						this.Width - ( ( m_int_CaptionHeight / 2 ) - ( m_Icon.Height / 2 ) ) - m_Icon.Width-5
						:
						this.Width)-5 , 
						// HEIGHT
						m_int_CaptionHeight ) 
						, format );
				}

				// draw the shadow
				Pen pen1 = new Pen( Color.FromArgb( 142 , 142 , 142 ) , 1.0f );
				Pen pen2 = new Pen( Color.FromArgb( 171 , 171 , 171 ) , 1.0f );
				Pen pen3 = new Pen( Color.FromArgb( 212 , 212 , 212 ) , 1.0f );
				Pen pen4 = new Pen( Color.FromArgb( 241 , 241 , 241 ) , 1.0f );

				e.Graphics.DrawLine( pen1 , this.Width - 5 , 2 , this.Width - 5 , this.Height - 5 );
				e.Graphics.DrawLine( pen2 , this.Width - 4 , 4 , this.Width - 4 , this.Height - 4 );
				e.Graphics.DrawLine( pen3 , this.Width - 3 , 6 , this.Width - 3 , this.Height - 3 );
				e.Graphics.DrawLine( pen4 , this.Width - 2 , 8 , this.Width - 2 , this.Height - 2 );

				e.Graphics.DrawLine( pen1 , 2 , this.Height - 5 , this.Width - 5 , this.Height - 5 );
				e.Graphics.DrawLine( pen2 , 4 , this.Height - 4 , this.Width - 4 , this.Height - 4 );
				e.Graphics.DrawLine( pen3 , 6 , this.Height - 3 , this.Width - 3 , this.Height - 3 );
				e.Graphics.DrawLine( pen4 , 8 , this.Height - 2 , this.Width - 2 , this.Height - 2 );
			}
			else
			{
				// fill the background
				System.Drawing.Drawing2D.LinearGradientBrush brsh = new System.Drawing.Drawing2D.LinearGradientBrush( new Rectangle( 0 , 0 , this.Width , this.Height ) , m_color_StartColor , m_color_EndColor , m_GradientMode );
				e.Graphics.FillRectangle( brsh , 0 , 0 , this.Width , this.Height );

				// draw the border
				switch( m_BorderStyle )
				{
					case GDFlacTools.Controls.BorderStyle.Single:
					{
						System.Drawing.Pen pen = new Pen( m_color_BorderColor );
						for( int i=0 ; i<m_int_BorderWidth ; i++ )
						{
							e.Graphics.DrawRectangle( pen , i , i , this.Width - 1 - (i*2) , this.Height - 1 - (i*2) );
						}
						break;
					}
					case GDFlacTools.Controls.BorderStyle.Raised3D:
					{
						break;
					}
				}

				// draw caption bar
				if( m_bool_Caption )
				{
					if( m_bool_Antialias ) e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
					System.Drawing.Drawing2D.LinearGradientBrush brsh_Caption = new System.Drawing.Drawing2D.LinearGradientBrush( new Rectangle( (m_BorderStyle != GDFlacTools.Controls.BorderStyle.None ) ?  m_int_BorderWidth : 0 , (m_BorderStyle != GDFlacTools.Controls.BorderStyle.None ) ?  m_int_BorderWidth : 0 , (m_BorderStyle != GDFlacTools.Controls.BorderStyle.None ) ?  this.Width - (m_int_BorderWidth*2) : this.Width , m_int_CaptionHeight ) , m_color_CaptionBeginColor , m_color_CaptionEndColor , m_CaptionGradientMode );
					e.Graphics.FillRectangle( brsh_Caption , (m_BorderStyle != GDFlacTools.Controls.BorderStyle.None ) ?  m_int_BorderWidth : 0 , (m_BorderStyle != GDFlacTools.Controls.BorderStyle.None ) ?  m_int_BorderWidth : 0 , (m_BorderStyle != GDFlacTools.Controls.BorderStyle.None ) ?  this.Width - (m_int_BorderWidth*2) : this.Width , m_int_CaptionHeight );
					StringFormat format = new StringFormat();
					format.FormatFlags = StringFormatFlags.NoWrap;
					format.LineAlignment = StringAlignment.Center;
					format.Alignment = m_StringAlignment;
					format.Trimming = StringTrimming.EllipsisCharacter;
					e.Graphics.DrawString( 
						m_str_Caption , 
						this.Font , 
						new SolidBrush( m_color_CaptionTextColor ) , 
						new Rectangle( 
							// LEFT
							(m_BorderStyle != GDFlacTools.Controls.BorderStyle.None ) ? 
							(m_bool_Icon ? 
								m_int_BorderWidth + m_Icon.Width + ( ( m_int_CaptionHeight / 2 ) - ( m_Icon.Height / 2 ) ) 
								: 
								m_int_BorderWidth)
							: 
							(m_bool_Icon ? 
								m_Icon.Width + ( ( m_int_CaptionHeight / 2 ) - ( m_Icon.Height / 2 ) ) 
								: 
								0) , 
							// TOP
							(m_BorderStyle != GDFlacTools.Controls.BorderStyle.None ) ?  
								m_int_BorderWidth 
								: 
								0 ,
							// WIDTH
							(m_BorderStyle != GDFlacTools.Controls.BorderStyle.None ) ?  
								(m_bool_Icon ? 
									this.Width - (m_int_BorderWidth*2) - ( ( m_int_CaptionHeight / 2 ) - ( m_Icon.Height / 2 ) ) - m_Icon.Width 
									: 
									this.Width - (m_int_BorderWidth*2))
								:
								(m_bool_Icon ?
									this.Width - ( ( m_int_CaptionHeight / 2 ) - ( m_Icon.Height / 2 ) ) - m_Icon.Width
									:
									this.Width) , 
							// HEIGHT
							m_int_CaptionHeight ) 
							, format );
				}
			}

			// draw the icon
			if( m_bool_Icon && m_bool_Caption )
			{
				e.Graphics.DrawIcon( m_Icon , ((m_BorderStyle != GDFlacTools.Controls.BorderStyle.None ) ? m_int_BorderWidth : 0 ) + ( ( m_int_CaptionHeight / 2 ) - ( m_Icon.Height / 2 ) ) , ((m_BorderStyle != GDFlacTools.Controls.BorderStyle.None ) ? m_int_BorderWidth : 0 ) + ( ( m_int_CaptionHeight / 2 ) - ( m_Icon.Height / 2 ) ) );
			}

			base.OnPaint (e);
		}
		#endregion

		#region Overrides
		protected override void OnResize(EventArgs e)
		{
			Invalidate();
			base.OnResize (e);
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		#endregion
	}
}
